home *** CD-ROM | disk | FTP | other *** search
Text File | 1999-06-16 | 23.4 KB | 903 lines | [TEXT/MPS ] |
- /*
- File: file.c
-
- Copyright: © 1997-1998 by Apple Computer, Inc., all rights reserved.
-
- */
-
- //
- // You may incorporate this sample code into your applications
- // without restriction. This sample code has been provided "AS
- // IS" and the responsibility for its operation is 100% yours.
- // You are not permitted to redistribute the source as "Apple
- // sample code" after having made changes. If you're going to
- // re-distribute the source, we require that you make it clear
- // in the source that the code was descended from Apple sample
- // code, but that you've made changes.
- //
-
- #pragma segment DocSeg
-
- #ifndef __STRINGS__
- #include <Strings.h>
- #endif
-
- #ifndef __STDIO__
- #include <StdIO.h>
- #endif
-
- #ifndef __STANDARDFILE__
- #include <StandardFile.h>
- #endif
-
- #ifndef __FINDER__
- #include <Finder.h>
- #endif
-
- #ifndef __SOUND__
- #include <Sound.h>
- #endif
-
- #ifndef __NAVIGATION__
- #include "Navigation.h"
- #endif
-
-
- #ifndef Common_Defs
- #include "Common.h"
- #endif
-
- const long kPictHeaderSize = 512;
-
- extern short gDocumentCount;
-
- short ReadFile(Document* theDocument);
- short WriteFile(Document* theDocument);
- short WriteNewFile(Document* theDocument, FSSpec* newFileSpec);
- short DoSaveAsDocument(Document* theDocument);
- short DoSaveAsDocumentOldWay(Document* theDocument);
-
- pascal Boolean myFilterProc(AEDesc* theItem, void* info, NavCallBackUserData callBackUD, NavFilterModes filterMode);
-
- extern Document* gDocumentList[kMaxDocumentCount];
- extern Boolean gNavServicesExists;
-
-
- // *****************************************************************************
- // *
- // * ReadFile()
- // *
- // *****************************************************************************
- short ReadFile(Document* theDocument)
- {
- long count;
- short theResult;
- char buffer[256];
- TextStyle theStyle;
-
- SetCursor(*GetCursor(watchCursor));
-
- if (theDocument->theTE != NULL)
- {
- TESetSelect(0,(**(theDocument->theTE)).teLength,theDocument->theTE);
- TEDelete(theDocument->theTE);
-
- if (theResult = SetFPos(theDocument->fRefNum,fsFromStart,0))
- return theResult;
-
- do {
- count = 256;
- theResult = FSRead(theDocument->fRefNum,&count,&buffer);
- TEInsert(&buffer,count,theDocument->theTE);
- }
- while (theResult == noErr);
-
- if (theResult == eofErr)
- theResult = noErr;
-
- TESetSelect(0,32767,theDocument->theTE);
- theStyle.tsFont = 21;
- theStyle.tsSize = 12;
- TESetStyle(doFont + doSize,&theStyle,true,theDocument->theTE);
- TESetSelect(0,0,theDocument->theTE);
- }
- else
- {
- short result = noErr;
- long fileSize = 0;
- long headerSize = 0;
- long pictSize = 0;
-
- SetCursor(*(Cursor**)GetCursor(watchCursor));
-
- if (theResult = SetFPos(theDocument->fRefNum,fsFromStart,0))
- return theResult;
-
- theResult = GetEOF(theDocument->fRefNum,&fileSize);
-
- theDocument->fPictLength = fileSize;
- theDocument->fPictLength -= kPictHeaderSize;
- theDocument->fPict = NewHandle(theDocument->fPictLength);
- theDocument->fHeader = NewHandle(kPictHeaderSize);
- if ((theDocument->fPict == NULL)||(theDocument->fPict == NULL))
- return memFullErr;
-
- headerSize = kPictHeaderSize;
- pictSize = theDocument->fPictLength;
-
- theResult = FSRead(theDocument->fRefNum,&headerSize,*theDocument->fHeader);
- theResult = FSRead(theDocument->fRefNum,&pictSize,*theDocument->fPict);
- }
-
- theDocument->dirty = false;
-
- if (theResult != noErr)
- {
- Str255 errorStr;
- GetIndString(errorStr,rAppStringsID,sReadErr);
- ParamText((ConstStr255Param)&errorStr,(ConstStr255Param)"\p",(ConstStr255Param)"\p",(ConstStr255Param)"\p");
- NoteAlert(rGenericAlertID,0L);
- }
- return theResult;
- }
-
-
- // *****************************************************************************
- // *
- // * WriteFile()
- // *
- // *****************************************************************************
- short WriteFile(Document* theDocument)
- {
- short theResult;
- long length;
- char* bufPtr;
-
- SetCursor(*GetCursor(watchCursor));
-
- if (!theDocument->fRefNum)
- return fnOpnErr;
-
- if (theResult = SetFPos(theDocument->fRefNum,fsFromStart,0))
- return theResult;
-
- if (theDocument->theTE != NULL)
- {
- length = (**(theDocument->theTE)).teLength;
- bufPtr = *((**(theDocument->theTE)).hText);
-
- theResult = FSWrite(theDocument->fRefNum,&length,bufPtr);
- if (theResult == noErr)
- theResult = SetEOF(theDocument->fRefNum,length);
- }
- else
- {
- long headerSize = kPictHeaderSize;
- long pictSize = theDocument->fPictLength;
-
- theResult = FSWrite(theDocument->fRefNum,&headerSize,*theDocument->fHeader);
- if (theResult == noErr)
- {
- theResult = FSWrite(theDocument->fRefNum,&pictSize,*theDocument->fPict);
- if (theResult == noErr)
- theResult = SetEOF(theDocument->fRefNum,headerSize+pictSize);
- }
- }
-
- return theResult;
- }
-
-
- // *****************************************************************************
- // *
- // * WriteNewFile()
- // *
- // *****************************************************************************
- short WriteNewFile(Document* theDocument, FSSpec* newFileSpec)
- {
- short theResult;
- short refNum = 0;
-
- SetCursor(*GetCursor(watchCursor));
-
- theResult = FSpOpenDF(newFileSpec,fsRdWrPerm,&refNum);
- if (refNum != -1)
- {
- if (theResult = SetFPos(refNum,fsFromStart,0))
- return(theResult);
-
- if (theDocument->theTE != NULL)
- {
- long length;
- char* bufPtr;
- length = (**(theDocument->theTE)).teLength;
- bufPtr = *((**(theDocument->theTE)).hText);
-
- if (theResult = FSWrite(refNum,&length,bufPtr))
- return(theResult);
-
- theResult = SetEOF(refNum,length);
- }
- else
- {
- long headerSize = kPictHeaderSize;
- long pictSize = theDocument->fPictLength;
-
- theResult = FSWrite(refNum,&headerSize,*theDocument->fHeader);
- theResult = FSWrite(refNum,&pictSize,*theDocument->fPict);
-
- theResult = SetEOF(refNum,headerSize+pictSize);
- }
- theResult = FSClose(refNum);
- }
- return theResult;
- }
-
-
- // *****************************************************************************
- // *
- // * DoNewDocument()
- // *
- // *****************************************************************************
- void DoNewDocument(Boolean newDocAsPICT)
- {
- Document* theDocument;
- if (theDocument = NewDocument(newDocAsPICT))
- ShowWindow(theDocument->theWindow);
- }
-
-
- // *****************************************************************************
- // *
- // * DoOpenFile()
- // *
- // *****************************************************************************
- OSErr DoOpenFile(FSSpec* theFile, Boolean openAsPICT)
- {
- OSErr result;
- short refNum;
- Document* theDocument = NULL;
-
- result = FSpOpenDF(theFile,fsRdWrPerm,&refNum);
- if ((result == fLckdErr) || (result == afpAccessDenied))
- result = FSpOpenDF(theFile,fsRdPerm,&refNum);
-
- if (result == noErr)
- {
- theDocument = NewDocument(openAsPICT);
- if (theDocument != NULL)
- {
- theDocument->fRefNum = refNum;
-
- result = ReadFile(theDocument);
- if (result == noErr)
- {
- SetWTitle(theDocument->theWindow,theFile->name);
- SizeDocWindow(theDocument);
-
- AdjustScrollBar(theDocument);
- ShowWindow(theDocument->theWindow);
- }
- }
- else
- {
- FSClose(refNum);
- result = memFullErr;
- }
- }
-
- if (result != noErr)
- {
- Str255 errorStr;
- if (result == vLckdErr || result == afpVolLocked || result == wPrErr || result == permErr)
- GetIndString(errorStr,rAppStringsID,sWriteToBusyFileErr);
- else
- if (result == opWrErr)
- GetIndString(errorStr,rAppStringsID,sBusyOpen);
- else
- if (result == memFullErr)
- GetIndString(errorStr,rAppStringsID,sLowMemoryErr);
- else
- GetIndString(errorStr,rAppStringsID,sOpeningErr);
- ParamText((ConstStr255Param)&errorStr,(ConstStr255Param)"\p",(ConstStr255Param)"\p",(ConstStr255Param)"\p");
- NoteAlert(rGenericAlertID,0L);
- }
-
- return result;
- }
-
-
- // *****************************************************************************
- // *
- // * myFilterProc()
- // *
- // *****************************************************************************
- pascal Boolean myFilterProc(AEDesc* theItem, void* info, NavCallBackUserData /*callBackUD*/, NavFilterModes /*filterMode*/)
- {
- OSErr theErr = noErr;
- Boolean display = true;
- NavFileOrFolderInfo* theInfo = (NavFileOrFolderInfo*)info;
-
- if ( theItem->descriptorType == typeFSS )
- if ( !theInfo->isFolder )
- {
- // use:
- // 'theInfo->fileAndFolder.fileInfo.finderInfo.fdType'
- // to check for the file type you want to filter.
- }
- return display;
- }
-
-
- // *****************************************************************************
- // *
- // * myEventProc()
- // *
- // *****************************************************************************
- pascal void myEventProc(const NavEventCallbackMessage callBackSelector,
- NavCBRecPtr callBackParms,
- NavCallBackUserData callBackUD)
- {
- WindowPtr pWindow = NULL;
- Document** docList;
- Document* theDoc = NULL;
- short index = 0;
-
- if (callBackUD != 0)
- switch (callBackSelector)
- {
- case kNavCBEvent:
- {
- docList = (Document**)callBackUD;
- if (docList != NULL)
- switch (callBackParms->eventData.event->what)
- {
- case nullEvent:
- break;
-
- case updateEvt:
- pWindow = (WindowPtr)callBackParms->eventData.event->message;
- theDoc = docList[index];
- if (theDoc != NULL)
- {
- while ((theDoc->theWindow != pWindow) && (docList[index] != NULL))
- {
- index++;
- theDoc = docList[index];
- }
- theDoc = docList[index];
- if (theDoc != NULL)
- UpdateWindow(theDoc);
- }
- break;
-
- case activateEvt:
- break;
-
- default:
- break;
- }
- break;
- }
- }
- }
-
-
- // *****************************************************************************
- // *
- // * DoOpenDocumentTheOldWay()
- // *
- // *****************************************************************************
- OSErr DoOpenDocumentTheOldWay()
- {
- OSErr theErr = noErr;
- SFTypeList theTypeList;
- StandardFileReply theReply;
-
- theTypeList[0] = 'TEXT';
- theTypeList[1] = 'PICT';
- StandardGetFile(0L,2,theTypeList,&theReply);
-
- if (theReply.sfGood)
- {
- if (theReply.sfType == 'TEXT')
- DoOpenFile(&theReply.sfFile,false);
- else
- DoOpenFile(&theReply.sfFile,true);
- }
- return theErr;
- }
-
-
-
-
- // *****************************************************************************
- // *
- // * DoOpenDocument()
- // *
- // *****************************************************************************
- OSErr DoOpenDocument()
- {
- NavReplyRecord theReply;
- NavDialogOptions dialogOptions;
- OSErr theErr = noErr;
- NavTypeListHandle openList = NULL;
- long count = 0;
- NavEventUPP eventUPP = NewNavEventProc(myEventProc);
- NavObjectFilterUPP filterUPP = NewNavObjectFilterProc(myFilterProc);
-
- // default behavior for browser and dialog:
- theErr = NavGetDefaultDialogOptions(&dialogOptions);
-
- GetIndString((unsigned char*)&dialogOptions.clientName,rAppStringsID,sApplicationName);
-
- openList = (NavTypeListHandle)GetResource(kOpenRsrcType,kOpenRsrcID);
-
- dialogOptions.preferenceKey = kOpenPrefKey;
-
- dialogOptions.dialogOptionFlags += kNavDontAutoTranslate; // we will do the translation ourselves later:
-
- theErr = NavGetFile(NULL, // use system's default location
- &theReply,
- &dialogOptions,
- eventUPP,
- NULL, // no custom previews
- filterUPP,
- (NavTypeListHandle)openList,
- (NavCallBackUserData)&gDocumentList);
-
- DisposeRoutineDescriptor(eventUPP);
- DisposeRoutineDescriptor(filterUPP);
-
- if (theReply.validRecord && theErr == noErr)
- {
- // since we allow for multiple objects to be returned,
- // grab the target FSSpecs from 'theReply.fileRef' list for opening:
- FSSpec finalFSSpec;
- AEDesc resultDesc;
- FInfo fileInfo;
-
- // in the case we didn't want built in translation:
- if ((dialogOptions.dialogOptionFlags & kNavDontAutoTranslate) != 0)
- if (theReply.translationNeeded)
- {
- // if we didn't want built in translation it was for the following reasons:
- // 1) we wanted to do it ourselves
- // 2) or we wanted to defer it
- // things to remember if auto-translation is turned off:
- // 1) the AEDesc list contains the original file specs the user had chosen.
- // 2) the 'fileTranslation' field for each object that needs translation has filled in for you.
-
- // put your own code here to perform your own translation.
- // - or -
- // we can simply call this to perform the translation manually:
- Str255 errorStr;
-
- if ((theErr = NavTranslateFile(&theReply,kNavTranslateCopy)) != noErr)
- {
- if (theErr == vLckdErr || theErr == afpVolLocked || theErr == wPrErr || theErr == permErr)
- GetIndString(errorStr,rAppStringsID,sTranslationLockedErr);
- else
- GetIndString(errorStr,rAppStringsID,sTranslationErr);
- ParamText((ConstStr255Param)&errorStr,(ConstStr255Param)"\p",(ConstStr255Param)"\p",(ConstStr255Param)"\p");
- NoteAlert(rGenericAlertID,0L);
- }
- }
-
- if (theErr == noErr)
- {
- // we are ready to open the document(s), grab information about each file for opening:
- theErr = AECountItems(&(theReply.selection),&count);
- for (long index=1;index<=count;index++)
- {
- resultDesc.dataHandle = 0L;
- if ((theErr = AEGetNthDesc(&(theReply.selection),index,typeFSS,NULL,&resultDesc)) == noErr)
- {
- BlockMoveData(*resultDesc.dataHandle,&finalFSSpec,sizeof(FSSpec));
-
- // decide if the doc we are opening is a 'PICT' or 'TEXT':
- if ((theErr = FSpGetFInfo(&finalFSSpec,&fileInfo)) == noErr)
- {
- if (fileInfo.fdType == kFileType)
- (void)DoOpenFile(&finalFSSpec,false);
- else
- if (fileInfo.fdType == kFileTypePICT)
- (void)DoOpenFile(&finalFSSpec,true);
- else
- {
- // error:
- // if we got this far, the document is a type we can't open and
- // (most likely) built-in translation was turned off.
- // You can alert the user that this returned selection or file spec
- // needs translation.
- }
- }
- theErr = AEDisposeDesc(&resultDesc);
- }
- }
- }
-
- theErr = NavDisposeReply(&theReply); // clean up after ourselves
- }
-
- if (openList != NULL)
- ReleaseResource((Handle)openList);
-
- return theErr;
- }
-
-
- // *****************************************************************************
- // *
- // * SaveACopyDocumentTheOldWay()
- // *
- // *****************************************************************************
- OSErr SaveACopyDocumentTheOldWay(Document* theDocument)
- {
- OSErr theErr = noErr;
- Str255 thePrompt, theName;
- StandardFileReply theReply;
-
- if (!theDocument)
- return false;
-
- GetIndString((unsigned char*)&thePrompt,rAppStringsID,sSaveCopyMessage);
- GetWTitle(theDocument->theWindow,(unsigned char*)&theName);
- StandardPutFile((unsigned char*)&thePrompt,(unsigned char*)&theName,&theReply);
-
- if (theReply.sfGood)
- {
- if (theReply.sfReplacing)
- theErr = FSpDelete(&theReply.sfFile);
-
- if ((theErr = FSpCreate(&theReply.sfFile,kFileCreator,'TEXT',smSystemScript)) == noErr)
- theErr = WriteNewFile(theDocument,&theReply.sfFile); // use this document's data to write to our new copy:
-
- if (theErr == fBsyErr)
- {
- Str255 errorStr;
- GetIndString(errorStr,rAppStringsID,sWriteToBusyFileErr);
- ParamText((ConstStr255Param)&errorStr,(ConstStr255Param)"\p",(ConstStr255Param)"\p",(ConstStr255Param)"\p");
- NoteAlert(rGenericAlertID,0L);
- }
- }
- else
- return false;
-
- return true;
- }
-
-
- // *****************************************************************************
- // *
- // * SaveACopyDocument()
- // *
- // *****************************************************************************
- OSErr SaveACopyDocument(Document* theDocument)
- {
- OSErr theErr = noErr;
- NavReplyRecord theReply;
- NavDialogOptions dialogOptions;
- NavEventUPP eventUPP = NewNavEventProc(myEventProc);
- Str255 docTitle;
- OSType fileTypeToSave;
-
- // default behavior for browser and dialog:
- theErr = NavGetDefaultDialogOptions(&dialogOptions);
-
- // user might want to translate the saveed doc into another format
- dialogOptions.dialogOptionFlags -= kNavDontAddTranslateItems;
-
- GetIndString((unsigned char*)&dialogOptions.clientName,rAppStringsID,sApplicationName);
-
- GetWTitle(theDocument->theWindow,docTitle);
- p2cstr((StringPtr)docTitle);
- sprintf((char*)dialogOptions.savedFileName,(char*)"%s copy",docTitle);
- c2pstr((Ptr)dialogOptions.savedFileName);
-
- if (theDocument->theTE != NULL) // which document type is it?
- fileTypeToSave = kFileType;
- else
- fileTypeToSave = kFileTypePICT;
-
- dialogOptions.preferenceKey = kSavePrefKey;
-
- theErr = NavPutFile(NULL, // use system's default location
- &theReply,
- &dialogOptions,
- eventUPP,
- fileTypeToSave,
- kFileCreator,
- (NavCallBackUserData)&gDocumentList);
-
- DisposeRoutineDescriptor(eventUPP);
-
- if (theReply.validRecord && theErr == noErr)
- {
- FSSpec finalFSSpec;
- AEDesc resultDesc;
- resultDesc.dataHandle = 0L;
-
- // retrieve the returned selection:
- // since only 1 selection is possible, we get the first AEDesc:
- if ((theErr = AEGetNthDesc(&(theReply.selection),1,typeFSS,NULL,&resultDesc)) == noErr)
- {
- BlockMoveData(*resultDesc.dataHandle,&finalFSSpec,sizeof(FSSpec));
-
- if (theReply.replacing)
- theErr = FSpDelete(&finalFSSpec);
- if (theErr == noErr)
- {
- if ((theErr = FSpCreate(&finalFSSpec,kFileCreator,fileTypeToSave,smSystemScript)) == noErr)
- if ((theErr = WriteNewFile(theDocument,&finalFSSpec)) == noErr) // use this document's data to write to our new copy
- {
- // translation may be needed for file we are saving a copy of,
- // when you save a copy, you should always "translate in place":
- theErr = NavCompleteSave(&theReply,kNavTranslateInPlace);
- }
- }
- else
- if (theErr == fBsyErr)
- {
- Str255 errorStr;
- GetIndString(errorStr,rAppStringsID,sReadErr);
- ParamText((ConstStr255Param)&errorStr,(ConstStr255Param)"\p",(ConstStr255Param)"\p",(ConstStr255Param)"\p");
- NoteAlert(rGenericAlertID,0L);
- }
-
- AEDisposeDesc(&resultDesc);
- }
- theErr = NavDisposeReply(&theReply);
- }
-
- return theErr;
- }
-
-
- // *****************************************************************************
- // *
- // * DoSaveAsDocument()
- // *
- // *****************************************************************************
- short DoSaveAsDocument(Document* theDocument)
- {
- OSErr theErr = noErr;
- short result = true;
- NavReplyRecord theReply;
- NavDialogOptions dialogOptions;
- NavEventUPP eventUPP = NewNavEventProc(myEventProc);
- OSType fileTypeToSave;
-
- // default behavior for browser and dialog:
- NavGetDefaultDialogOptions(&dialogOptions);
-
- // user might want to translate the saveed doc into another format
- dialogOptions.dialogOptionFlags -= kNavDontAddTranslateItems;
-
- GetWTitle(theDocument->theWindow,dialogOptions.savedFileName);
- GetIndString((unsigned char*)&dialogOptions.clientName,rAppStringsID,sApplicationName);
-
- if (theDocument->theTE != NULL) // which document type is it?
- fileTypeToSave = kFileType;
- else
- fileTypeToSave = kFileTypePICT;
-
- dialogOptions.preferenceKey = kSavePrefKey;
-
- theErr = NavPutFile(NULL, // use system's default location
- &theReply,
- &dialogOptions,
- eventUPP,
- fileTypeToSave,
- kFileCreator,
- (NavCallBackUserData)&gDocumentList);
- DisposeRoutineDescriptor(eventUPP);
-
- if (theReply.validRecord && theErr == noErr)
- {
- FSSpec finalFSSpec;
- AEDesc resultDesc;
- resultDesc.dataHandle = 0L;
-
- // retrieve the returned selection:
- if ((theErr = AEGetNthDesc(&(theReply.selection),1,typeFSS,NULL,&resultDesc)) == noErr)
- {
- BlockMoveData(*resultDesc.dataHandle,&finalFSSpec,sizeof(FSSpec));
-
- if (!theReply.replacing)
- {
- result = FSpCreate(&finalFSSpec,kFileCreator,fileTypeToSave,theReply.keyScript);
- if (result)
- {
- SysBeep(5);
- return false;
- }
- }
-
- if (theDocument->fRefNum)
- result = FSClose(theDocument->fRefNum);
-
- result = FSpOpenDF(&finalFSSpec,fsRdWrPerm,&theDocument->fRefNum);
- if (result)
- {
- SysBeep(5);
- return false;
- }
-
- if (result = WriteFile(theDocument))
- return false;
-
- AEDisposeDesc(&resultDesc);
-
- theErr = NavCompleteSave(&theReply,kNavTranslateInPlace);
-
- SetWTitle(theDocument->theWindow,(unsigned char*)finalFSSpec.name);
- theDocument->dirty = false;
- }
-
- NavDisposeReply(&theReply);
- }
- else
- return false;
-
- return result;
- }
-
-
- // *****************************************************************************
- // *
- // * DoSaveAsDocumentOldWay()
- // *
- // *****************************************************************************
- short DoSaveAsDocumentOldWay(Document* theDocument)
- {
- short theResult;
- Str255 thePrompt, theName;
- StandardFileReply theReply;
- OSType fileTypeToSave;
-
- if (!theDocument)
- return false;
-
- GetIndString((unsigned char*)&thePrompt,rAppStringsID,slSavePromptIndex);
- GetWTitle(theDocument->theWindow,(unsigned char*)&theName);
- StandardPutFile((unsigned char*)&thePrompt,(unsigned char*)&theName,&theReply);
-
- if (theDocument->theTE != NULL) // which document type is it?
- fileTypeToSave = kFileType;
- else
- fileTypeToSave = kFileTypePICT;
-
- if (theReply.sfGood)
- {
- if (!theReply.sfReplacing)
- {
- theResult = FSpCreate(&theReply.sfFile,kFileCreator,fileTypeToSave,theReply.sfScript);
- if (theResult)
- {
- SysBeep(5);
- return false;
- }
- }
-
- if (theDocument->fRefNum)
- theResult = FSClose(theDocument->fRefNum);
-
- theResult = FSpOpenDF(&theReply.sfFile,fsRdWrPerm,&theDocument->fRefNum);
- if (theResult)
- {
- SysBeep(5);
- return false;
- }
-
- if (theResult = WriteFile(theDocument))
- {
- SysBeep(5);
- return false;
- }
-
- SetWTitle(theDocument->theWindow,(unsigned char*)theReply.sfFile.name);
- theDocument->dirty = false;
- }
- else
- return false;
-
- return true;
- }
-
-
- // *****************************************************************************
- // *
- // * DoSaveDocument()
- // *
- // *****************************************************************************
- short DoSaveDocument(Document* theDocument)
- {
- short theErr = noErr;
- if (theDocument != NULL)
- {
- if (theDocument->fRefNum)
- {
- if (WriteFile(theDocument))
- {
- SysBeep(5);
- return false;
- }
- else
- theDocument->dirty = false;
- theErr = true;
- }
- else
- {
- // need to save file for the first time:
- if (gNavServicesExists)
- theErr = DoSaveAsDocument(theDocument);
- else
- theErr = DoSaveAsDocumentOldWay(theDocument);
-
- if (theErr == memFullErr)
- theErr = DoSaveAsDocumentOldWay(theDocument);
- }
- }
- return theErr;
- }
-
-
- // *****************************************************************************
- // *
- // * DoRevertDocument()
- // *
- // *****************************************************************************
- void DoRevertDocument(Document* theDocument)
- {
- if (theDocument != NULL)
- if (theDocument->fRefNum)
- {
- OSErr theErr = noErr;
- NavEventUPP eventUPP = NewNavEventProc(myEventProc);
- NavAskDiscardChangesResult reply;
- NavDialogOptions dialogOptions;
-
- GetWTitle(theDocument->theWindow,dialogOptions.savedFileName);
- theErr = NavAskDiscardChanges( &dialogOptions,
- &reply,
- eventUPP,
- (NavCallBackUserData)&gDocumentList);
- DisposeRoutineDescriptor(eventUPP);
-
- switch (reply)
- {
- case kNavAskDiscardChanges:
- if (ReadFile(theDocument))
- SysBeep(5);
- break;
-
- case kNavAskDiscardChangesCancel:
- break;
- }
- }
- }
-
-
- // *****************************************************************************
- // *
- // * DoRevertDocumentTheOldWay()
- // *
- // *****************************************************************************
- void DoRevertDocumentTheOldWay(Document* theDocument)
- {
- Str255 theName;
-
- if (!theDocument)
- return;
-
- if (theDocument->fRefNum)
- {
- GetWTitle(theDocument->theWindow,(unsigned char*)&theName);
- ParamText((ConstStr255Param)&theName,(ConstStr255Param)"\p",(ConstStr255Param)"\p",(ConstStr255Param)"\p");
- if (Alert(rRevertID,0L) == 1)
- if (ReadFile(theDocument))
- SysBeep(5);
- }
- }
-